package com.ruoyi.web.controller.test;

import com.ruoyi.common.config.JwtConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.jwt.JwtUtils;
import com.ruoyi.framework.jwt.service.JwtTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * JWT测试控制器
 * 
 * @author ruoyi
 */
@Api(tags = "JWT测试接口")
@RestController
@RequestMapping("/test")
public class JwtTestController extends BaseController
{
    @Autowired
    private JwtConfig jwtConfig;
    
    @Autowired
    private JwtTokenService jwtTokenService;
    /**
     * 测试接口 - GET方式
     * 
     * @param name 姓名
     * @return 当前时间 + hello name + token信息
     */
    @ApiOperation("测试接口")
    @GetMapping("/hello")
    public AjaxResult hello(@ApiParam(value = "姓名", required = true) @RequestParam(name = "name") String name, HttpServletRequest request)
    {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String currentTime = now.format(formatter);
        
        // 获取Token信息
        String token = getTokenFromRequest(request);
        String tokenInfo = "";
        boolean isTokenValid = false;
        if (StringUtils.isNotEmpty(token))
        {
            // 使用封装的Token验证方法
            JwtUtils.TokenValidationResult result = JwtUtils.validateTokenWithDetails(token, jwtConfig.getSecret(), jwtTokenService::isTokenBlacklisted);
            tokenInfo = result.getTokenInfo();
            isTokenValid = result.isValid();
        }
        else
        {
            tokenInfo = " [未提供Token]";
        }
        
        // 构造返回消息
        String message = currentTime + " hello " + name + "!" + tokenInfo;
        
        // 根据token状态返回不同的响应
        if (!isTokenValid)
        {
            return AjaxResult.error("Token验证失败").put("code", 401).put("data", message);
        }
        
        return AjaxResult.success("请求成功", message);
    }
    
    /**
     * 测试接口 - POST方式
     * 
     * @param name 姓名
     * @return 当前时间 + hello name + token信息
     */
    @ApiOperation("测试接口 - POST")
    @PostMapping("/hello")
    public AjaxResult helloPost(@ApiParam(value = "姓名", required = true) @RequestParam(name = "name") String name, HttpServletRequest request)
    {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String currentTime = now.format(formatter);
        
        // 获取Token信息
        String token = getTokenFromRequest(request);
        String tokenInfo = "";
        boolean isTokenValid = false;
        if (StringUtils.isNotEmpty(token))
        {
            // 使用封装的Token验证方法
            JwtUtils.TokenValidationResult result = JwtUtils.validateTokenWithDetails(token, jwtConfig.getSecret(), jwtTokenService::isTokenBlacklisted);
            tokenInfo = result.getTokenInfo();
            isTokenValid = result.isValid();
        }
        else
        {
            tokenInfo = " [未提供Token]";
        }
        
        // 构造返回消息
        String message = currentTime + " hello " + name + "!" + tokenInfo;
        
        // 根据token状态返回不同的响应
        if (!isTokenValid)
        {
            return AjaxResult.error("Token验证失败").put("code", 401).put("data", message);
        }
        
        return AjaxResult.success("请求成功", message);
    }
    
    /**
     * 从请求中获取Token
     * 
     * @param request 请求对象
     * @return Token字符串
     */
    private String getTokenFromRequest(HttpServletRequest request)
    {
        String token = request.getHeader("Authorization");
        if (StringUtils.isNotEmpty(token) && token.startsWith("Bearer "))
        {
            return token.substring("Bearer ".length());
        }
        return null;
    }
}